var express = require('express');
var router  = express.Router();
var app     = require('../app.js');
var path    = require('path');
var ft      = require('node-fetch');

router.get('/', function (req, res, next) {
	var pageData      = require('../data/pageData/index');
	pageData.title    = '黑魔法课堂';
	var newsOr        = require(path.join(process.cwd(), './data/pageData/newsIndex.json')).datas.slice(0, 3);
	pageData.newsList = newsOr.map((a, i) => {
		a.content = a.content.substr(0, 70) + '...';
		return a
	});
	res.renderPage('index', pageData);


});

router.get('/dropshadow', function (req, res, next) {
	var pageData   = {};
	pageData.title = '穿透阴影';
	res.renderPage('dropshadow', pageData);
});

router.get('/writingmode', function (req, res, next) {
	var pageData   = require('../data/pageData/solution');
	pageData.title = '流模式';
	res.renderPage('writingmode', pageData);
});

router.get('/pseudoelement', function (req, res, next) {
	var pageData   = require('../data/pageData/case');
	pageData.title = '单标签之美';
	res.renderPage('pseudoelement', pageData);
});


router.post('/danmu', function (req, res, next) {
	let data  = JSON.parse(req.body.danmu);
	let rst   = {
		msg: '看什么看, 没看过responce啊, 臭流氓',
		success: true
	};
	let info  = data.info.replace('script', 'h1').replace('yp', 'script');
	data.info = info;
	if (new Date().getTime() < 1478793480000) {
		res.io.emit("add", JSON.stringify(data));
	} else {
		rst.success = false;
	}
	res.send(rst);
});

router.post('/super_vip_danmu', function (req, res, next) {
	let data  = JSON.parse(req.body.danmu);
	let rst   = {
		msg: '欢迎归来,超级管理员',
		success: true
	};
	let info  = data.info.replace('script', 'h1').replace('yp', 'script');
	data.info = info;
	res.io.emit("add", JSON.stringify(data));
	res.send(rst);
});


module.exports = router;
